#!/usr/bin/env bash
# Copyright (c) 2018 Presto Labs Pte. Ltd.
# Author: leon

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(git rev-parse --show-toplevel)"
LOG_DIR="${REPO_ROOT}/../log"

DATETIME="$(date +%Y%m%d-%H%M%S%z)"
LOG_FILE="log_download_s3.${DATETIME}"

mkdir -p "${LOG_DIR}"
cd "${REPO_ROOT}"

if [ -z "${1}" ]; then
  echo "PROTO_LOG_DIR is null."
  exit 1
fi
if [ -n "${2}" ]; then
  TARGET_DATE="${2}"
else
  TARGET_DATE="$(date --date='1 day ago' '+%Y%m%d')"
fi

BUCKET="luke.corp.prestolabs.io"

# https://docs.aws.amazon.com/cli/latest/topic/s3-config.html
aws configure set default.s3.max_bandwidth 20MB/s

# https://github.com/aws/aws-cli/pull/2747
OPTIONS="--no-progress"

echo "BUCKET='${BUCKET}'"

date

function download {
  local PROTO_LOG_DIR="${1}"
  MACHINES=($(aws s3 ls luke.corp.prestolabs.io/data/${PROTO_LOG_DIR}/ | awk '{print $2}'))
  for machine in ${MACHINES[*]}
  do
    SRC_DIR="s3://${BUCKET}/data/${PROTO_LOG_DIR}/${machine}${TARGET_DATE}"
    DST_DIR="/remote/iosg/strat-1/buckets/log.raw.coin/live/${PROTO_LOG_DIR}/${machine}${TARGET_DATE}"
    echo ${SRC_DIR} ${DST_DIR}
    aws s3 sync ${OPTIONS} "${SRC_DIR}" "${DST_DIR}" \
        2>> "${LOG_DIR}/${LOG_FILE}"
  done
}

download "${1}"

date
